package com.cn.dim.controller;

import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.dim.pojo.User;
import com.cn.dim.service.AuthenticationService;
import com.cn.dim.utils.BaseUtil;
import com.cn.dim.utils.Md5Util;
import authentication.dao.TokenPersistent;
import authentication.entiy.Token;
import authentication.manager.TokenManager;
import authentication.manager.factory.TokenManagerFactory;



@Controller
@RequestMapping("/authentication")
public class AuthenticationController {
	@Resource
	private TokenPersistent tokenPersistent;//令牌dao
	@Resource
	private AuthenticationService authenticationService;
	
	@RequestMapping(value = "/applogin")
	public @ResponseBody Map<String,Object> createToken(HttpServletRequest request,HttpServletResponse response,String userName,String password) throws IOException, NoSuchAlgorithmException {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		Token token=new Token();
		User userInfo = new User();
		if(password==null || "".equals(password.trim())){
			//普通员工
			params.put("username", userName);
			userInfo = authenticationService.login(params);//调用登录方法判断登录是否成功
			if(userInfo!=null){
				String ip=BaseUtil.getIpAddr(request);
				Date nowTime = Calendar.getInstance().getTime();
				SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
				String  time=formatter.format(nowTime); 
				InputStream in = this.getClass().getClassLoader().getResourceAsStream("token.properties");
				Properties props = new Properties();
				props.load(in);
				String INVALID_TIME = props.getProperty("INVALID_TIME");
				
				token.setClientIp(ip);
				token.setCreateDate(time);
				token.setRequestDate(time);
				token.setTokenId(BaseUtil.makeUUID());
				token.setVaildDate(INVALID_TIME);
				token.setUserName(userName);
				token.setUserId(userInfo.getId());
				TokenManager tokenManager = TokenManagerFactory.getInstance();
				String tokenSequence=tokenManager.createTokenSequence(token,tokenPersistent);
				map.put("tokenSequence", tokenSequence);
				if(tokenSequence!=null){
					map.put("status", "success");
					map.put("message", "登录成功");
				} else {
					map.put("status", "error");
					map.put("message", "令牌未生成");
				}
			}else{
				map.put("status", "error");
				map.put("message", "员工编号与姓名不匹配！");
				return map;
			}
		
		}else{
			params.put("telephone", userName);
			userInfo = authenticationService.telephonelogin(params);//调用登录方法判断登录是否成功
			if(userInfo!=null){
				boolean passTransMd5 = Md5Util.validPassword(password, userInfo.getPassword());
				if(!passTransMd5){
					map.put("status", "error");
					map.put("message", "手机号或密码不正确！");
					return map;
				}
				String ip=BaseUtil.getIpAddr(request);
				Date nowTime = Calendar.getInstance().getTime();
				SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
				String  time=formatter.format(nowTime); 
				InputStream in = this.getClass().getClassLoader().getResourceAsStream("token.properties");
				Properties props = new Properties();
				props.load(in);
				String INVALID_TIME = props.getProperty("INVALID_TIME");
				
				token.setClientIp(ip);
				token.setCreateDate(time);
				token.setRequestDate(time);
				token.setTokenId(BaseUtil.makeUUID());
				token.setVaildDate(INVALID_TIME);
				token.setUserName(userName);
				token.setUserId(userInfo.getId());
				TokenManager tokenManager = TokenManagerFactory.getInstance();
				String tokenSequence=tokenManager.createTokenSequence(token,tokenPersistent);
				map.put("tokenSequence", tokenSequence);
				if(tokenSequence!=null){
					map.put("status", "success");
					map.put("message", "登录成功");
				} else {
					map.put("status", "error");
					map.put("message", "令牌未生成");
				}
			}else{
				map.put("status", "error");
				map.put("message", "手机号或密码不正确！");
				return map;
			}
		}
		
		//用户名密码登录方式   实际中还需要扩展openid 方式登录
		
		return map;
	}

}
